<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/24
 * Time: 8:17
 */
namespace app\api\controller;
use think\Db;
use think\Request;
use app\api\model\Method as Met;
class Method extends Nocommon
{
    /*
     * 攻略列表
     */
    public function methodList()
    {
        if(Request::instance()->isPost()){

            $content = input('content','','trim');
            $where['title'] = array('like','%' . $content . '%');
            $page = $this->p();
            $method = new Met();

            $data['read_sum'] = $method->method_sum();
            $data['read_all'] = $method->read_all();
            $data['list'] = $method->method_list($where,$page);
            $this->apiReturn('200', '成功', $data);
        }
    }
    /**
     * [methodInfo 攻略详情]
     * @Author   kendy
     * @DateTime 2019-10-24T11:38:20+0800
     * @return   [type]                   [description]
     */
    public function methodInfo()
    {
         if(Request::instance()->isPost()){  
          $groupTour = new Met(); 
          $mid = input('post.mid/d');  
          $uid = input('post.uid/d');    
          if(empty($mid)) return $this->apiReturn('400','攻略id为空',0);
          $GroupTourInfo = $groupTour->getMethodInfo($mid,$uid);  
          $GroupTourInfo['content'] = json_decode($GroupTourInfo['content'],true);  
          if(count($GroupTourInfo['content'])>0){
              foreach ($GroupTourInfo['content'] as $key => $value) { 
                 $GroupTourInfo['content'][$key]['image'] = addWaterByField($value['image']);
              }
          }
          return $this->apiReturn('200','成功',$GroupTourInfo);
        }

    }
    /**
     * [selectTour 写攻略（选择景点）]
     * @Author   kendy
     * @DateTime 2019-10-25T09:59:44+0800
     * @return   [type]                   [description]
     */
    
    public function addMethod()
    {
       if(Request::instance()->isPost()){
           $view_id        = input('post.view_id/s');
           if(empty($view_id))  return $this->apiReturn('400','景点id为空',0);
           $view_name        = input('post.view_name/s');
           if(empty($view_name))  return $this->apiReturn('400','景点名称为空',0);
           $title        = input('post.title/s');
           if(empty($title))  return $this->apiReturn('400','标题为空',0);
           $content        = input('post.content');
           if(empty($content)) return $this->apiReturn('400','内容为空',0); 
           $userId        = input('post.uid/d');
           if(empty($userId))  return $this->apiReturn('400','用户信息为空',0);
           $image        = input('post.image/s'); 
           /*
           [{"type":"\u5f20\u96ea\u6885","age":"27","content":"\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f"},{"type":"\u5f20\u6c9b\u9716","age":"21","content":"\u8f6f\u4ef6\u5de5\u7a0b"}]
            */  
            $image =  [];
            $data =  [];
            $contents = json_decode($content);   
            if(count($contents)>0)
            {
                 foreach($contents as $key => $value) { 
                  if($value->image)
                  {
                     $image[] = ($value->image);
                  }  
                   if($value->data)
                  {
                     $data[] = ($value->data);
                  }  
                  
                }  
            } 
           if(count($image)>0)
           $image =  implode(',', $image);
           if(count($data)>0) 
           $text =  implode(',', $data); 
           $data = [
            'view_id'  =>  $view_id,
            'view_name' => $view_name,
            'title' =>  $title,
            'content' =>  $content,
            'image' =>  $image,
            'uid' =>   $userId,
            'is_pass' =>  '0',
            'create_time' =>  date('Y-m-d H:i:s'),
            'mtext' =>$text
        ];
          $groupTour = new Met();      
          $resultData = $groupTour->addMethod($data);  
          return $this->apiReturn('200','数据插入成功',0);
        }

    }
    
}